package com.sgb.trip.view.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.sgb.link.entity.trip.FlightSearchEntity;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripPopwindowFlightDetailInfoBinding;
import com.sgb.trip.entity.FlightInfoEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.NumberUtil;

public class FlightChosenDetailPopWindow extends BasePopWindow implements View.OnClickListener {
    TripPopwindowFlightDetailInfoBinding mBinding;

    FlightInfoEntity mEntity;

    FlightSearchEntity mEntitySearch;

    FlightInfoEntity mEntityTo;

    public FlightChosenDetailPopWindow(Context context) {
        super(context, DIR_CENTER);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    public void setEntitySearch(FlightSearchEntity entitySearch) {
        this.mEntitySearch = entitySearch;
    }

    public void setEntityTo(FlightInfoEntity entityTo) {
        this.mEntityTo = entityTo;
    }

    public void setEntityBack(FlightInfoEntity entityBack) {
        this.mEntity = entityBack;
    }

    @Override
    protected int popLayout() {
        return R.layout.trip_popwindow_flight_detail_info;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (TripPopwindowFlightDetailInfoBinding) holder.getmBinding();
        mBinding.tvConfirmPassenger.setOnClickListener(view -> dismiss());
    }

    public void setDataShow() {
        if (mEntitySearch != null) {
            if (mEntitySearch.isSingle()) {

                mBinding.tvTo.setVisibility(View.GONE);
                mBinding.rlChooseBack.setVisibility(View.GONE);
                if (mEntityTo != null) {
                    setData(mEntityTo);
                }
            } else {
                mBinding.tvTo.setVisibility(View.VISIBLE);
                if (mEntitySearch.isBack()) {
                    mBinding.rlChooseBack.setVisibility(View.VISIBLE);
                    setBackData(mEntity);
                    setData(mEntityTo);
                } else {
                    mBinding.rlChooseBack.setVisibility(View.GONE);
                    if (mEntity != null) {
                        setData(mEntity);
                    }
                }
            }
        }
    }

    /**
     * 设置去程和单程的数据
     *
     * @param entity
     */
    private void setData(FlightInfoEntity entity) {
        if (mEntitySearch != null) {
            String builder = DateUtils.formatDate(mEntitySearch.getStartDateStamp(), "MM月dd日") +
                    " " +
                    "星期" + mEntitySearch.getStartWeek() +
                    "   " +
                    mEntitySearch.getStartCity() +
                    " → " +
                    mEntitySearch.getArriveCity();
            mBinding.tvTimeCity.setText(builder);
        }
        if (entity != null) {
            mBinding.tvStartAirport.setText(entity.getFromAirportName() + entity.getFromTerminal());
            mBinding.tvArriveAirport.setText(entity.getToAirportName() + entity.getToTerminal());
            mBinding.tvFlightName.setText(entity.getAirlineCompany() + entity.getFlightNo());
            mBinding.tvFlightCode.setText(entity.getCraftType());
            mBinding.tvFlightFood.setText(entity.getMeals());

            if (!entity.getFromDate().equals(entity.getToDate())) {
                mBinding.tvPlusDay.setVisibility(View.VISIBLE);
            } else {
                mBinding.tvPlusDay.setVisibility(View.GONE);
            }
            mBinding.tvStartTime.setText(entity.getFromDateTime());
            mBinding.tvArriveTime.setText(entity.getToDateTime());

            mBinding.tvDuration.setText(String.format("%sh%sm", NumberUtil.trim0(entity.getFlyDuration().split(":")[0], 0),
                    NumberUtil.trim0(entity.getFlyDuration().split(":")[1], 0)));
            Glide.with(mContext).load(entity.getLogo()).into(mBinding.ivLogo);

        }

    }

    /**
     * 设置返程数据
     *
     * @param entity
     */
    private void setBackData(FlightInfoEntity entity) {
        if (mEntitySearch != null) {
            String builder = DateUtils.formatDate(mEntitySearch.getReturnDateStamp(), "MM月dd日") +
                    " " +
                    "星期" + mEntitySearch.getBackWeek() +
                    "   " +
                    mEntitySearch.getArriveCity() +
                    " → " +
                    mEntitySearch.getStartCity();
            mBinding.tvTimeCityBack.setText(builder);
        }
        if (entity != null) {
            mBinding.tvStartAirportBack.setText(entity.getFromAirportName() + entity.getFromTerminal());
            mBinding.tvArriveAirportBack.setText(entity.getToAirportName() + entity.getToTerminal());
            mBinding.tvFlightNameBack.setText(entity.getAirlineCompany() + entity.getFlightNo());
            mBinding.tvFlightCodeBack.setText(entity.getCraftType());
            mBinding.tvFlightFoodBack.setText(entity.getMeals());

            if (!mEntity.getFromDate().equals(entity.getToDate())) {
                mBinding.tvPlusDayBack.setVisibility(View.VISIBLE);
            } else {
                mBinding.tvPlusDayBack.setVisibility(View.GONE);
            }
            mBinding.tvStartTimeBack.setText(entity.getFromDateTime());
            mBinding.tvArriveTimeBack.setText(entity.getToDateTime());

            mBinding.tvDurationBack.setText(String.format("%sh%sm", NumberUtil.trim0(entity.getFlyDuration().split(":")[0], 0),
                    NumberUtil.trim0(entity.getFlyDuration().split(":")[1], 0)));
            Glide.with(mContext).load(entity.getLogo()).into(mBinding.ivLogoBack);
        }
    }


    @Override
    public void onClick(View view) {

    }
}
